package com.jt.service;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jt.mapper.ItemDescMapper;
import com.jt.pojo.Item;
import com.jt.pojo.ItemDesc;
import com.jt.vo.EasyUITable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jt.mapper.ItemMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
public class ItemServiceImpl  implements ItemService {

    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private ItemDescMapper itemDescMapper;


    //分页
    @Override
    public EasyUITable findItemByPagePackage(int page, int rows) {
        long total = itemMapper.countItemByPage();
        //2.手写分页
        int start = (page - 1) * rows;
        List<Item> itemList = itemMapper.findItemByPage(start,rows);

        return new EasyUITable(total,itemList);


    }






    @Override
    @Transactional
    public void updateItem(Item item) {
        itemMapper.updateById(item);
    }

    @Override
    @Transactional
    public void deleteItem(Long... ids) {
//        itemMapper.deleteBatchIds(Arrays.asList(ids));
        itemMapper.deleteItem(ids);
        itemDescMapper.deleteBatchIds(Arrays.asList(ids));
    }


    @Override
    @Transactional
    public void updateStatus(Long[] ids, Integer status) {
       /* Item item=new Item();
        item.setStatus(status);
        UpdateWrapper updateWrapper=new UpdateWrapper();
        updateWrapper.in("id",Arrays.asList(ids));
        itemMapper.update(item,updateWrapper);*/
       itemMapper.updateStatus(ids,status);
    }

    @Override
    @Transactional
    public void saveItem(Item item, ItemDesc itemDesc) {
        item.setStatus(1);
        itemMapper.insert(item);

        itemDesc.setItemId(item.getId());
        itemDescMapper.insert(itemDesc);

    }

    @Override
    public ItemDesc findItemById(Long itemId) {
        return itemDescMapper.selectById(itemId);
    }




}
